//
//  DDDExpressFullScreenVC.m
//  DDDAdDemo
//
//  Created by mu on 2020/7/27.
//  Copyright © 2020 summer. All rights reserved.
//

#import "DDDExpressFullScreenVC.h"
#import <DDDAdSdk/DDDAdSdk.h>
#import "MBProgressHUD.h"

@interface DDDExpressFullScreenVC ()<DDDNativeExpressFullScreenVideoAdDelegate>
@property (nonatomic, strong) DDDNativeExpressFullScreenVideoAd *fullscreenAd;
@property (nonatomic, strong) UIButton *upBtn;
@property (nonatomic, strong) UIButton *downBtn;
@end

@implementation DDDExpressFullScreenVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    CGFloat nextBtnW = 280.0;
    CGFloat nextBtnH = 45.0;
    CGFloat nextBtnX = ([UIScreen mainScreen].bounds.size.width - nextBtnW) * 0.5;
    CGFloat nextBtnY = 150.0;
    [self.view addSubview:self.upBtn];
    self.upBtn.frame = CGRectMake(nextBtnX, nextBtnY, nextBtnW, nextBtnH);
    
    nextBtnY = nextBtnY + nextBtnH + 20.0;
    [self.view addSubview:self.downBtn];
    self.downBtn.frame = CGRectMake(nextBtnX, nextBtnY, nextBtnW, nextBtnH);
    
    switch (self.type) {
        case 0:
            //竖屏
        {
            self.navigationItem.title = @"全屏竖屏广告";
            DDDNativeExpressFullScreenVideoAd *fullscreenAd = [[DDDNativeExpressFullScreenVideoAd alloc]initWithType:DDDFullScreenType_VerticalVedio];
            fullscreenAd.delegate = self;
            self.fullscreenAd = fullscreenAd;
        }
            break;
        case 1:
            //横屏
        {
            self.navigationItem.title = @"全屏横屏广告";
            DDDNativeExpressFullScreenVideoAd *fullscreenAd = [[DDDNativeExpressFullScreenVideoAd alloc]initWithType:DDDFullScreenType_HorizontalVedio];
            fullscreenAd.delegate = self;
            self.fullscreenAd = fullscreenAd;
        }
            break;
        default:
            break;
    }
}
#pragma mark-- 点击事件
- (void)clickBtnSetAction:(UIButton *)sender {
    if (self.fullscreenAd) {
        [self.fullscreenAd loadAdData];
    }
}

- (void)clickBtnShowAction:(UIButton *)sender {
    if (self.fullscreenAd && self.fullscreenAd.isAdValid) {
        [self.fullscreenAd showAdRootViewController:self.navigationController];
    } else {
        NSLog(@"视频播放还没有缓存成功");
    }
}

#pragma mark-- 懒加载
- (UIButton *)upBtn {
    if (!_upBtn) {
        _upBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_upBtn setTitle:@"获取视频" forState:UIControlStateNormal];
        [_upBtn addTarget:self action:@selector(clickBtnSetAction:) forControlEvents:UIControlEventTouchUpInside];
        [_upBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_upBtn setBackgroundColor:[UIColor redColor]];
        _upBtn.layer.masksToBounds = YES;
        _upBtn.layer.cornerRadius = 8.0;
    }
    return _upBtn;
}

- (UIButton *)downBtn {
    if (!_downBtn) {
        _downBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_downBtn setTitle:@"展示视频" forState:UIControlStateNormal];
        [_downBtn addTarget:self action:@selector(clickBtnShowAction:) forControlEvents:UIControlEventTouchUpInside];
        [_downBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_downBtn setBackgroundColor:[UIColor redColor]];
        _downBtn.layer.masksToBounds = YES;
        _downBtn.layer.cornerRadius = 8.0;
    }
    return _downBtn;
}
#pragma mark-- DDDNativeExpressFullScreenVideoAdDelegate
/// 当视频广告素材成功加载时的回调
- (void)nativeExpressFullscreenVideoAdDidLoad {
    NSLog(@"%s",__func__);
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.offset = CGPointMake(0, -100);
    hud.label.text = @"广告素材加载成功";
    [hud hideAnimated:YES afterDelay:2];
}

/// 当加载视频广告材料失败时回调。
/// @param errorCode 错误码
- (void)nativeExpressFullscreenVideoAdDidFailWithError:(NSInteger)errorCode {
    NSLog(@"%s",__func__);
    NSLog(@"summer--%ld",errorCode);
}


/// 呈现广告成功时的回调
- (void)nativeExpressFullscreenVideoAdViewRenderSuccess {
    NSLog(@"%s",__func__);
}


/// 广告无法呈现时的回调
/// @param errorCode 错误码
- (void)nativeExpressFullscreenVideoAdViewRenderFailError:(NSInteger)errorCode {
    NSLog(@"%s",__func__);
    NSLog(@"summer--%ld",errorCode);
}


/// 广告缓存成功时的回调 （可以在这里调用显示视频）
- (void)nativeExpressFullscreenVideoAdDidDownLoadVideo {
    NSLog(@"%s",__func__);
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.offset = CGPointMake(0, -100);
    hud.label.text = @"广告素材可以播放了";
    [hud hideAnimated:YES afterDelay:2];
    
}


/// 广告即将显示时回调
- (void)nativeExpressFullscreenVideoAdWillVisible {
    NSLog(@"%s",__func__);
}

/// 广告已经显示的回调
- (void)nativeExpressFullscreenVideoAdDidVisible {
    NSLog(@"%s",__func__);
}

/// 点击广告时的回调
- (void)nativeExpressFullscreenVideoAdDidClick {
    NSLog(@"%s",__func__);
}

/// 单机跳过按钮的回调
- (void)nativeExpressFullscreenVideoAdDidClickSkip {
    NSLog(@"%s",__func__);
}

/// 广告即将关闭时的回调
- (void)nativeExpressFullscreenVideoAdWillClose {
    NSLog(@"%s",__func__);
}

/// 广告关闭时的回调
- (void)nativeExpressFullscreenVideoAdDidClose {
    NSLog(@"%s",__func__);
}

/// 广告播放完成或者出现错误后的回调
/// @param errorCode 错误码
- (void)nativeExpressFullscreenVideoAdDidPlayFinishDidFailWithError:(NSInteger)errorCode {
    NSLog(@"%s",__func__);
    NSLog(@"summer--%ld",errorCode);
}

/// 此方法在关闭另一个控制器时调用
- (void)nativeExpressFullscreenVideoAdDidCloseOtherController {
    NSLog(@"%s",__func__);
}

@end
